//
//  XiaoBianMoreCombineView.m
//  瓜子电台
//
//  Created by lanou on 16/8/6.
//  Copyright © 2016年 YueZhang. All rights reserved.
//

#import "XiaoBianMoreCombineView.h"

@interface XiaoBianMoreCombineView()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation XiaoBianMoreCombineView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        self.scrollView.pagingEnabled = YES;
        self.scrollView.scrollEnabled = NO;
        self.scrollView.contentSize = CGSizeMake(2 * kScreenWidth, 0);
        self.scrollView.contentOffset = CGPointMake(kScreenWidth, 0);
        [self createTableView];
    }
    return self;
}

- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self addSubview:self.tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_combine"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell_combine"];
    }
    [cell addSubview:self.scrollView];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.scrollView.ownHeight;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    self.seg = [[UISegmentedControl alloc] initWithItems:@[@"详情", @"节目"]];
    self.seg.frame = CGRectMake(0, 0, kScreenWidth, 40);
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17], NSForegroundColorAttributeName:[UIColor redColor]};
    self.seg.tintColor = [UIColor clearColor];
    [self.seg setTitleTextAttributes:dic forState:(UIControlStateSelected)];
    NSDictionary *dic1 = @{NSFontAttributeName:[UIFont systemFontOfSize:17],
                           NSForegroundColorAttributeName:[UIColor blackColor]};
    [self.seg setTitleTextAttributes:dic1 forState:(UIControlStateNormal)];
    
    [self.seg addTarget:self action:@selector(segAction) forControlEvents:(UIControlEventValueChanged)];
    
    self.lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 38, 34, 2)];
    self.lineView.backgroundColor = [UIColor redColor];
    self.lineView.center = CGPointMake(self.seg.centerX / 2 + kScreenWidth / 2, 38);
    [self.seg addSubview:self.lineView];
    self.seg.backgroundColor = [UIColor whiteColor];
    return _seg;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}

- (void) segAction
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationRepeatCount:1];
    self.lineView.center = CGPointMake(kScreenWidth / 2 * self.seg.selectedSegmentIndex + self.seg.centerX / 2, 38);
    [UIView commitAnimations];
    CGPoint x = CGPointMake(kScreenWidth * self.seg.selectedSegmentIndex, 0);
    [self.scrollView setContentOffset:x animated:YES];
}




@end
